// pages/search/search.js
var {getSearch} = require("../../api/search.js");
var {getGoodsDetail} = require("../../api/Home.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:"",
    isshow:false,
    historylist:[],
    searchlist:[],
    lists:[]
  },
  getGoodsDetailFun(){
    let likelist = wx.getStorageSync('searchlist');
    let lists = [];
    likelist.forEach(item=>{
      getGoodsDetail({"goods_id":item}).then(data=>{
        if(data.message.goods_small_logo){
          lists.push(data.message);
        }        
        this.setData({
          lists:lists
        })  
      })
    })   
  },
  touchitem(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      value:this.data.historylist[index]
    })
  },
  clear(){
    this.setData({
      historylist:[]
    })
  },
  inputtap(){
    wx.removeStorageSync('searchlist');
    this.setData({
      isshow:false
    })
  },
  search(){  
    let value = this.data.value;
    let historylist = this.data.historylist;
    let searchlist = [];
    if(this.data.value !=""){
      getSearch({query:value}).then(data=>{
        data.message.forEach(item=>{
          searchlist.push(item.goods_id);
        })
        wx.setStorageSync('searchlist', searchlist);
        historylist.push(this.data.value);      
        this.setData({
          historylist:historylist,
          value:"",
          isshow:true
        })
        this.getGoodsDetailFun();
      })
      
    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})